package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TestCasePlan;
import com.ruoyi.system.mapper.TestCasePlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TestStep;
import com.ruoyi.system.mapper.TestCaseMapper;
import com.ruoyi.system.domain.TestCase;
import com.ruoyi.system.service.ITestCaseService;
import com.ruoyi.common.core.text.Convert;

/**
 * 测试用例Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-25
 */
@Service
public class TestCaseServiceImpl implements ITestCaseService 
{
    @Autowired
    private TestCaseMapper testCaseMapper;

    @Autowired
    private TestCasePlanMapper testCasePlanMapper;

    /**
     * 查询测试用例
     * 
     * @param id 测试用例主键
     * @return 测试用例
     */
    @Override
    public TestCase selectTestCaseById(Long id)
    {
        return testCaseMapper.selectTestCaseById(id);
    }

    /**
     * 查询测试用例列表
     * 
     * @param testCase 测试用例
     * @return 测试用例
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TestCase> selectTestCaseList(TestCase testCase)
    {
        return testCaseMapper.selectTestCaseList(testCase);
    }

    /**
     * 新增测试用例
     * 
     * @param testCase 测试用例
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTestCase(TestCase testCase)
    {
        testCase.setCreateTime(DateUtils.getNowDate());
        int rows = testCaseMapper.insertTestCase(testCase);
        insertTestStep(testCase);
        return rows;
    }

    /**
     * 修改测试用例
     * 
     * @param testCase 测试用例
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTestCase(TestCase testCase)
    {
        testCase.setUpdateTime(DateUtils.getNowDate());
        testCaseMapper.deleteTestStepByCaseId(testCase.getId());
        insertTestStep(testCase);
        return testCaseMapper.updateTestCase(testCase);
    }

    /**
     * 批量删除测试用例
     * 
     * @param ids 需要删除的测试用例主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestCaseByIds(String ids)
    {
        testCaseMapper.deleteTestStepByCaseIds(Convert.toStrArray(ids));
        return testCaseMapper.deleteTestCaseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除测试用例信息
     * 
     * @param id 测试用例主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestCaseById(Long id)
    {
        testCaseMapper.deleteTestStepByCaseId(id);
        return testCaseMapper.deleteTestCaseById(id);
    }

    /**
     * 新增用例步骤信息
     * 
     * @param testCase 测试用例对象
     */
    public void insertTestStep(TestCase testCase)
    {
        List<TestStep> testStepList = testCase.getTestStepList();
        Long id = testCase.getId();
        if (StringUtils.isNotNull(testStepList))
        {
            List<TestStep> list = new ArrayList<TestStep>();
            for (TestStep testStep : testStepList)
            {
                testStep.setCaseId(id);
                list.add(testStep);
            }
            if (list.size() > 0)
            {
                testCaseMapper.batchTestStep(list);
            }
        }
    }

    @Override
    public List<TestCase> seletAllocatedList(TestCase testCase){
        return testCaseMapper.selectAllocatedList(testCase);
    }

    @Override
    public List<TestCase> selectUnallocatedList(TestCase testCase) {
        return testCaseMapper.selectUnallocatedList(testCase);
    }

    @Override
    public void checkPlanDataScope(Long planId) {

    }

    @Override
    public int insertAuthCases(Long planId, String caseIds) {
        Long[] cases = Convert.toLongArray(caseIds);
        // 新增用户与角色管理
        List<TestCasePlan> list = new ArrayList<TestCasePlan>();
        for (Long caseId : cases)
        {
            TestCasePlan ur = new TestCasePlan();
            ur.setCaseId(caseId);
            ur.setPlanId(planId);
            list.add(ur);
        }
        return testCasePlanMapper.batchCasePlan(list);
    }
}
